package com.yxh.lib_base.ext.view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.vectordrawable.graphics.drawable.Animatable2Compat
import com.bumptech.glide.Glide
import com.bumptech.glide.integration.webp.decoder.WebpDrawable
import com.bumptech.glide.integration.webp.decoder.WebpDrawableTransformation
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.Transformation
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.CenterInside
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.yxh.lib_base.ext.xpopup.builder

/**
 * 作者 : yuan_xh
 * 时间 : 2022/8/25 13:19
 * 说明 :
 */


fun ImageView.loadResWebp(@DrawableRes resId: Int) {
    //webp动图
    val transformation: Transformation<Bitmap> = CenterInside()
    Glide.with(this)
        .load(resId) //不是本地资源就改为url即可
        .optionalTransform(transformation)
        .optionalTransform<WebpDrawable>(
            WebpDrawable::class.java,
            WebpDrawableTransformation(transformation)
        )
        .into(this)
}

fun ImageView.loadResWebp(@DrawableRes resId: Int, onAnimationEnd: () -> Unit) {
    loadWebp(resId) {
        registerAnimationCallback(object : Animatable2Compat.AnimationCallback() {
            override fun onAnimationStart(drawable: Drawable) {
                super.onAnimationStart(drawable)
            }

            override fun onAnimationEnd(drawable: Drawable) {
                super.onAnimationEnd(drawable)
                onAnimationEnd.invoke()
                unregisterAnimationCallback(this)
            }
        })
    }
}


fun ImageView.loadWebp(@DrawableRes resId: Int, webpDrawable: WebpDrawable.() -> Unit) {
    //webp动图
    val transformation: Transformation<Bitmap> = CenterInside()
    Glide.with(this)
        .load(resId) //不是本地资源就改为url即可
        .optionalTransform(transformation)
        .optionalTransform<WebpDrawable>(
            WebpDrawable::class.java,
            WebpDrawableTransformation(transformation)
        ).addListener(object : RequestListener<Drawable?> {
            override fun onLoadFailed(
                e: GlideException?,
                model: Any?,
                target: Target<Drawable?>?,
                isFirstResource: Boolean,
            ): Boolean {
                return false
            }

            override fun onResourceReady(
                resource: Drawable?,
                model: Any?,
                target: Target<Drawable?>?,
                dataSource: DataSource?,
                isFirstResource: Boolean,
            ): Boolean {
                val mDrawable = resource as WebpDrawable
                //需要设置为循环1次才会有onAnimationEnd回调
                mDrawable.loopCount = 1
                webpDrawable.invoke(mDrawable)
                return false
            }
        }).into(this)
}
